<template>
<!-- 基础题库 -->
  <div class="user_box">
    <!-- 顶部搜索 -->
    <div class="jichu_top">
      <el-form
        ref="elForm"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-row :gutter="20">
          <el-col :span="12"
            ><div class="red_text">
              说明：目前支持学科和关键字条件筛选
            </div></el-col
          >
          <el-col :span="12"
            ><div class="add">
              <el-button
                class="addBtn"
                type="primary"
                icon="el-icon-edit"
                @click="$router.push('/questions/new')"
                >新增试题</el-button
              >
            </div></el-col
          >
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="学科">
                <el-select
                  v-model="formInline.subject"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options4"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="二级目录">
                <el-select
                  v-model="formInline.erji"
                  placeholder="请选择"
                  style="width: 100%"
                >
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="标签">
                <el-select
                  v-model="formInline.tags"
                  placeholder="请选择"
                  style="width: 100%"
                >
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="关键字">
                <el-input v-model="formInline.question"></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="试题类型">
                <el-select
                  v-model="formInline.questionType"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options1"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="难度">
                <el-select
                  v-model="formInline.difficulty"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options2"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="方向">
                <el-select
                  v-model="formInline.direction"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options3"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="录入人">
                <el-select
                  v-model="formInline.creator"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options5"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form-item label="题目备注">
                <el-input v-model="form.type"></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form-item label="企业简称">
                <el-input v-model="form.type"></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item label="城市" class="city">
                <el-select
                  v-model="formInline.city"
                  placeholder="请选择"
                  style="width: 50%"
                  @change="open(value8)"
                >
                  <el-option
                    v-for="item in city"
                    :key="item.city"
                    :label="item.city"
                    :value="item.city"
                  >
                  </el-option>
                </el-select>
                <el-select
                  v-model="value9"
                  placeholder="请选择"
                  style="width: 50%"
                >
                  <el-option
                    v-for="item in area"
                    :key="item"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="6"
            ><div class="grid-content bg-purple">
              <el-form-item>
                <el-button @click="inputClick">清空</el-button>
                <el-button type="primary" @click="onSerch">搜索</el-button>
              </el-form-item>
            </div></el-col
          >
        </el-row>
      </el-form>
    </div>

    <!-- 中间分隔 -->
    <div class="m_alert">
      <i class="el-icon-info"></i>
      <div class="m_text">
        共<span>{{ total }}</span
        >条数据
      </div>
    </div>

    <!-- table表单 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="104">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="104">
        </el-table-column>
        <el-table-column prop="catalog" label="目录" width="104">
        </el-table-column>
        <el-table-column prop="questionType" label="题型" width="104">
        </el-table-column>
        <el-table-column prop="question" label="题干" width="104">
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="104">
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="104">
        </el-table-column>
        <el-table-column prop="creator" label="录入人" width="104">
        </el-table-column>

        <!--右侧标签 默认插槽 -->
        <el-table-column label="操作" width="260">
          <template #default="{ row }">
            <!-- 预览 -->
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              class="edit1"
              @click="viewBtn('edit', row.id)"
            ></el-button>
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit2"
              @click="editBtn('edit', row.id)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="primary"
              icon="el-icon-delete"
              circle
              class="edit3"
              @click="delBtn(row.id)"
            ></el-button>
          <el-button
              v-if="row.state"
              type="primary"
              icon="el-icon-close"
              circle
              class="edit5"
              @click="statueBtn(row)"
            ></el-button>
            <el-button
              v-else
              type="primary"
              icon="el-icon-check"
              circle
              class="edit4"
              @click="statueBtn(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页导航 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          :current-page="quaryObj.page"
          :page-sizes="[1, 3, 5, 10]"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </template>
  </div>
</template>

<script>
import { list } from '@/api/hmmm/questions.js'
// import { datas } from '@/api/hmmm/citys.js'
import { add, remove, update, detail, disabled } from '@/api/hmmm/companys.js'
export default {
  // components: {
  //   navbar
  // },
  data () {
    return {
      labelPosition: 'right',
      formLabelAlign: {
        name: '',
        region: '',
        type: ''
      },
      city: '',
      area: '',
      options1: [
        {
          value: '选项1',
          label: '单选'
        },
        {
          value: '选项2',
          label: '多选'
        },
        {
          value: '选项3',
          label: '简答'
        }
      ],
      options2: [
        {
          value: '选项1',
          label: '简单'
        },
        {
          value: '选项2',
          label: '一般'
        },
        {
          value: '选项3',
          label: '困难'
        }
      ],
      options3: [
        {
          value: '选项1',
          label: 'o2o'
        },
        {
          value: '选项2',
          label: '外包服务'
        },
        {
          value: '选项3',
          label: '企业服务'
        },
        {
          value: '选项4',
          label: '互联网金融'
        },
        {
          value: '选项5',
          label: '企业咨询'
        },
        {
          value: '选项6',
          label: '互联网'
        },
        {
          value: '选项7',
          label: '电子商务'
        },
        {
          value: '选项8',
          label: '其他'
        }
      ],
      options4: [],
      options5: [
        {
          value: '选项1',
          label: '超级管理员'
        },
        {
          value: '选项2',
          label: 'dome'
        }
      ],
      value1: '',
      value2: '',
      value3: '',
      value4: '',
      value5: '',
      value6: '',
      value7: '',
      value8: '',
      value9: '',

      statue: 1,
      datas: '',
      citys: [],
      areas: '',
      statues: [
        {
          value: '选项1',
          label: '启用'
        },
        {
          value: '选项2',
          label: '禁用'
        }
      ],
      value: '',

      id: null,
      total: 0,
      dialogType: 'create',
      rules: {
        shortName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        city: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        company: [
          { required: true, message: '请输入所属公司', trigger: 'blur' }
        ],
        tags: [{ required: true, message: '请输入标签', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入', trigger: 'blur' }]
      },

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        type: '',
        page: '',
        pagesize: '',
        subjectID: '',
        difficulty: '',
        questionType: '',
        tags: '',
        province: '',
        city: '',
        keyword: '',
        remarks: '',
        shortName: '',
        direction: '',
        creatorID: '',
        catalogID: ''
      },
      formLabelWidth: '100px',

      quaryObj: {
        page: 1,
        pagesize: 10
      },
      formInline: {
        type: '',
        page: '',
        pagesize: '',
        subjectID: '',
        difficulty: '',
        questionType: '',
        tags: '',
        province: '',
        city: '',
        keyword: '',
        remarks: '',
        shortName: '',
        direction: '',
        creatorID: '',
        catalogID: ''
      },
      tableData: []
    }
  },

  created () {
    // this.datas = datas
    this.renderList()
  },

  methods: {
    // 提示对话框
    open () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    openState () {
      this.$confirm('状态已改变, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '状态改变成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '状态改变失败'
          })
        })
    },

    async statueBtn (row) {
      this.openState()
      row.state = !row.state
    },
    // 搜索表单的城市
    getAreas () {
      // console.log(this.formInline.province)
      const data = this.datas.filter(item => {
        return item.city === this.formInline.province
      })
      this.areas = data[0].area
      this.formInline.city = this.areas
    },
    // 新增表单的城市
    getAddAreas () {
      // console.log(this.formInline.province)
      const data = this.datas.filter(item => {
        return item.city === this.form.province
      })
      this.areas = data[0].area
      this.form.city = this.areas
    },
    // dialog 新增用户的功能
    dialogBtn (type) {
      this.dialogType = type
      // if (this.dialogType === 'create') {
      this.dialogFormVisible = true
    },
    cancelBtn () {
      this.dialogType = 'create'
      this.clearAddform()
      this.dialogFormVisible = false
    },

    clearAddform () {
      this.form.tags = ''
      this.form.province = ''
      this.form.city = ''
      this.form.shortName = ''
      this.form.state = ''
      this.form.company = ''
      this.form.remarks = ''
    },
    // 表单点击确认
    async addForm () {
      try {
        if (this.dialogType === 'create') {
          // this.formInline.province = this.form.province
          // this.formInline.city = this.form.city
          console.log(this.form)
          await add(this.form)
        }
        if (this.dialogType === 'edit') {
          await update({ id: this.id, ...this.form })
          this.renderList()
          // console.log(res)
        }
        this.clearAddform()
        this.dialogFormVisible = false
        this.$message.success(
          this.dialogType === 'create' ? '添加成功' : '编辑成功'
        )
        this.renderList()
      } catch (error) {
        this.$message.error('添加失败')
      }
    },

    // 渲染页面
    async renderList () {
      const { data } = await list(this.quaryObj)
      console.log(data)
      this.tableData = [...data.items]
      this.total = data.counts
      // this.form.state = data.items.state
    },
    // 分页
    handleSizeChange (pageSize) {
      this.quaryObj.pagesize = pageSize
      // console.log(`每页 ${pageSize} 条`)
      this.renderList()
    },
    handleCurrentChange (current) {
      this.quaryObj.page = current
      // console.log(`当前页: ${current}`)
      this.renderList()
    },

    // 修改按钮
    async editBtn (type, id) {
      this.id = id
      this.dialogType = type
      const { data } = await detail(id)
      this.$router.push({
        name: 'questions-new',
        params: { data }
      })
      // console.log(data)
      // this.form.number = data.number
      // this.form.id = data.id
      // this.form.tags = data.tags
      // this.form.province = data.province
      // this.form.city = data.city
      // this.form.shortName = data.shortName
      // this.form.company = data.company
      // this.form.remarks = data.remarks
      // this.dialogFormVisible = true
    },

    // 删除
    async delBtn (id) {
      try {
        await remove(id)
        this.$message.success('删除成功')
        this.renderList()
        // console.log(data)
      } catch (error) {
        this.$message.error('删除失败')
      }
    },

    // 清空搜索框
    inputClick () {
      console.log(this.formInline)
      this.formInline.tags = ''
      this.formInline.province = ''
      this.formInline.city = ''
      this.formInline.shortName = ''
      this.formInline.difficulty = ''
      this.formInline.questionType = ''
      this.formInline.city = ''
      this.formInline.keyword = ''
      this.formInline.remarks = ''
      this.formInline.shortName = ''
      this.formInline.direction = ''
      this.formInline.type = ''
      this.formInline.type = ''
      this.formInline.type = ''
      this.renderList()
    },
    // 搜索
    onSerch () {
      if (!this.formInline.userInput) return this.$message.error('不能为空')
      const data = this.tableData.filter(item => {
        return item.username === this.formInline.userInput
      })
      this.tableData = data
    }
  },
  computed: {
    dialogTitle () {
      if (this.dialogType === 'create') return '添加用户'
      if (this.dialogType === 'statue') return '修改状态'
      if (this.dialogType === 'edit') return '修改用户'
      else {
        return '添加用户'
      }
    }
  }
}
</script>

<style scoped lang="less">
.jichu_top {
  .red_text {
    font-size: 12px;
    color: pink;
  }
}
.mid_title {
  margin: 15px 0;
}
.user_box {
  // width: 1188px;
  width: 100%;
  // height: 417.5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  margin: 20px;
  padding: 20px;

  .demo-form-inline {
    .form_item {
      width: 25%;
      height: 50px;
      float: left;
      box-sizing: border-box;
    }
    span {
      line-height: 32px;
      text-align: right;
      vertical-align: middle;
      float: left;
      font-size: 14px;
      color: #2a2b2b;
      box-sizing: border-box;
      padding: 0px 12px 0px 0px;
      padding-right: 12px;
      // width: 80px;
      // height: 32px;
      // padding: 12px;
    }
  }
}
.addBtn {
  float: right;
  margin-left: 10px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border: 1px solid #dcdfe6;
  font-weight: 500;
}
.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin: 15px 0;
  border-radius: 4px;
  color: #909399;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.actions {
  width: 60px;
  height: 36px;
  margin-right: 5px;
  // padding-left: 5px;
  // padding-right: 5px;
  .el-icon {
    width: 14px;
    height: 14px;
    font-size: 14px;
    padding: 10px;

    line-height: 1;
    border-radius: 50%;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    transition: 0.1s;
    font-weight: 500;
  }
  .el-icon-edit {
    margin: 0 5px;
    margin-top: 6px;
    color: #409eff;
    background: #ecf5ff;
    border-color: #b3d8ff;
  }
  .el-icon-delete {
    margin-right: 5px;
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
  }
}
.edit1 {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  &:hover {
    background-color: #409eff;
    color: #fff;
  }
}
.edit2 {
  color: #67c23a;
  background: #f0f9eb;
  border-color: #c2e7b0;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit3 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit4 {
  color: #e6a23c;
  background: #fdf6ec;
  border-color: #f5dab1;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit5 {
  color: #e6a23c;
  background: #fdf6ec;
  border-color: #f5dab1;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}

// 顶部搜索
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
// .bg-purple-dark {
//   background: #99a9bf;
// }
// .bg-purple {
//   background: #d3dce6;
// }
// .bg-purple-light {
//   background: #e5e9f2;
// }
// .grid-content {
//   border-radius: 4px;
//   min-height: 36px;
// }
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
